package io.renren.modules.paper.controller;

import io.renren.common.utils.R;
import io.renren.modules.course.service.ExamCourseService;
import io.renren.modules.paper.entity.Paper;
import io.renren.modules.paper.entity.PaperData;
import io.renren.modules.paper.entity.PaperTitle;
import io.renren.modules.paper.service.PaperQueService;
import io.renren.modules.paper.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Yang
 * @Date 2019/9/4 18:48
 */
@RestController
@RequestMapping("/paper")
public class PaperController {

    @Autowired
    private PaperService paperService;

    @Autowired
    PaperQueService paperQueService;

    @Autowired
    ExamCourseService examCourseService;

    /***
     * 查询全部试卷
     * @author Yang
     * @date 2019/9/4 19:04
     * @return : io.renren.common.utils.R
     */
    @GetMapping("/findByAll")
    public R findByAll(){
        final List<Map<String, Object>> list = paperService.listMaps();
        return R.ok().put("list",list);
    }

    /***
     * 通过id查询试卷问题详情
     * @author Yang
     * @date 2019/9/5 14:55
     * @param paperId :
     * @return : io.renren.common.utils.R
     */
    @GetMapping("/getPaperByPaperId")
    public R getPaperByPaperId(int paperId){
        final Paper byId = paperService.getById(paperId);
        return R.ok().put("byId", byId);
    }

    //  删除选中试卷信息
    @GetMapping(value = "/deletePaper")
    public R deletePaper(Integer paper_id){
        paperService.deletePaper(paper_id);
        return R.ok();
    }

    //    获取搜索试卷列表信息
    @GetMapping("/searchPapersList")
    public R searchPapersList(@RequestParam("paperName")String paperName,
                              @RequestParam("courseId")Integer courseId,
                              @RequestParam("paperType")Integer paperType){
        List<Map<String, Object>> papersList = paperService.searchPapersList(paperName, courseId, paperType);
        return R.ok().put("查询", papersList);
    }

    //  通过cId获取科目下的所有问题
    @GetMapping("/getPaperQueDetailByCId")
    public R getPaperQueDetailByLangId(@RequestParam("cid")Integer cid){
        Map<String, Object> resultMap = paperService.getPaperQueDetailByCId(cid);
        return R.ok().put("data",resultMap);
    }

    @GetMapping("/findPaperById")
    public R findPaperById(Integer paperId){
        Paper byId = paperService.getById(paperId);
        return R.ok().put("byId", byId);
    }

    //  请求随机组卷，插入试卷数据，即发布试卷
    @PostMapping(value = "/randomInsertPaperInfo")
    public R randomInsertPaperInfo(PaperTitle temp){
            int result = paperService.randomInsertPaperInfo(temp);
            if (result >0) {
                return R.ok().put("试卷发布成功",result);
            } else {
                return R.error("数据库错误，试卷发布失败");
            }

    }

    //  请求固定组卷，插入试卷数据，即发布试卷
    @PostMapping(value = "/fixedInsertPaperInfo")
    public R fixedInsertPaperInfo(PaperData temp){
        int result = paperService.fixedInsertPaperInfo(temp);
        if (result >0) {
            return R.ok().put("试卷发布成功",result);
        } else {
            return R.error("数据库错误，试卷发布失败");
        }
    }

}
